requireJS(['constant', 'site', 'handlebars','public'], function (Constant, site, Handlebars, Public) {

    Public.initApplyTerm();
    //日期控件初始化
    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        pickerPosition: 'bottom',
    })

    var url = {
        selectLoanMethodList: "manager/selectLoanMethodList.do",
        badDebtsDtailsList:"manager/badDebtsDtailsList.do"
    }

    var frontData = {
        pageNum : 1
    }

    //查询坏账列表
    function salesManCheck() {
        var data = {
            pageNum: frontData.pageNum,
        }
        if ($("#start_time").val()) {
            data.start_time = $("#start_time").val()
        }
        if ($("#end_time").val()) {
            data.end_time = $("#end_time").val()
        }
        if ($("#seleNames option:selected").val()) {
            data.type = $("#seleNames option:selected").val()
        }
        if ($("#inputNames").val()) {
            data.keyword = $("#inputNames").val();
        }
        if ($("#at_id option:selected").val()) {
            data.atId = $("#at_id option:selected").val()
        }
        if ($("#loan_way option:selected").val()) {
            data.loan_way = $("#loan_way option:selected").val()
        }
        if ($("#product_choose_list").val()) {
            data.product_id = $("#product_choose_list").val()
        }
        site.send({
            url: url.badDebtsDtailsList,
            data: data,
            callback: function (data) {
                if (data && data !== "noLogin") {
                    data = JSON.parse(data)
                    if (data.type == 1) {
                        if (data.content) {
                            $('.bad-page-container').createPager({
                                current: frontData.pageNum,
                                total: data.content.total,
                                pageSize: data.content.pageSize,
                                needInput: false,
                                backFn: function (current) {
                                    frontData.pageNum = current;
                                    salesManCheck();
                                }
                            })
                            $('.result').html(data.content.total ? data.content.total : 0);
                        }
                        //handlebars模板引擎
                        var source = $("#badDebtsDtails_content_tpl").html()
                        var template = Handlebars.compile(source)
                        var content = data.content;

                        var menuIndex = getAurlparam('h') || 0;
                        var thirdMenusIndex = getAurlparam('t') || 0;
                        var leftIndex = getAurlparam('i') || 0;

                        var storageContent = JSON.parse(localStorage.getItem('content'));

                        var obj = {
                            content: content,
                            fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                        }
                        var html = template(obj)
                        $('#badDebtsDtails_content').html(html)

                        //复选框一个没选中则全选去除
                        $.each($("input[name='ch_id']"), function (i, e) {
                            var flag = false;
                            $(e).on('click', function () {
                                var _checked = $("input[name='ch_id_all']")
                                var _checkedAll = $("input[name='ch_id']")
                                $.each(_checkedAll, function (ii, ee) {
                                    if ($(ee).prop('checked')) {
                                        flag = true
                                    }
                                    else {
                                        flag = false
                                        return false;
                                    }
                                })
                                _checked.prop('checked', flag);
                            })
                        })
                    } else {
                        Constant.fn.Message("F", data.msg);
                    }
                }
            },
            callError: function () {
                alert("error")
            }
        })
    }


    //渲染搜索框的贷款方式
    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('#loan_way').append(html);
            }
        }
    })

    salesManCheck();

    $(".search").on('click',function(){
        salesManCheck();
    })
})